#include "../../global/QmlNames.h"
#include "../../persist/DbManager.h"
#include "GroupModel.h"

#include <QSqlQuery>

X_BEGIN_NAMESPACE

// private data
GroupModelPrivate::GroupModelPrivate()
    : m_photo("qrc:/images/avatar_default_group.png")
    , m_mute(false)
{
}

// Group Model
const char GroupModel::READ_MEMBERS[] = "select bareJid, nick_name, photo, photo_type from group_members m where m.group_id = :group_id";

GroupModel::GroupModel()
    : QObject(NULL)
    , d(new GroupModelPrivate)
{
}

GroupModel::GroupModel(const QString &id, QDateTime createTime)
    : QObject(NULL)
    , d(new GroupModelPrivate)
{
    d->m_id = id;
    d->m_createTime = createTime;
}

GroupModel::GroupModel(const GroupModel &src)
    : QObject(NULL)
    , d(src.d)
{
}

GroupModel &GroupModel::operator =(const GroupModel &rhs)
{
    if(this != &rhs) this->d = rhs.d;

    return *this;
}

GroupMemberModel &GroupModel::addMember(const QString &bareJid, const QString &name, const QString &photo, PhotoType photoType)
{
    return d->m_members.addMember(bareJid, name, photo, photoType);
}

const char *GroupModel::qmlName()
{
    return QmlNames::SubModelType::GroupModel;
}

QString GroupModel::photo() const
{
    return d->m_photo;
}

QString GroupModel::name() const
{
    return d->m_name;
}

QDateTime GroupModel::createTime() const
{
    return d->m_createTime;
}

QVariant GroupModel::members()
{
    // 成员延迟加载
    if(d->m_members.rowCount(QModelIndex()) == 0){
        QSqlQuery query = DbManager::instance().createQuery();
        query.prepare(READ_MEMBERS);
        query.bindValue(":group_id", d->m_id);
        query.exec();

        while (query.next()) {
            // bareJid, nick_name, photo, photo_type
            QString bareJid = query.value("bareJid").toString();
            QString nickName = query.value("nick_name").toString();
            QString photo = query.value("photo").toString();
            PhotoType photoType = PhotoType(query.value("photo_type").toInt());

            d->m_members.addMember(bareJid, nickName, photo, photoType);
        }
    }

    return QVariant::fromValue(&(d->m_members));
}

bool GroupModel::mute() const
{
    return d->m_mute;
}

PhotoType GroupModel::photoType() const
{
    return d->m_photoType;
}

QString GroupModel::id() const
{
    return d->m_id;
}

GroupModel& GroupModel::setPhoto(QString photo)
{
    if (d->m_photo == photo)
        return *this;

    d->m_photo = photo;
    emit photoChanged(photo);

    return *this;
}

GroupModel& GroupModel::setName(QString name)
{
    if (d->m_name == name)
        return *this;

    d->m_name = name;
    emit nameChanged(name);

    return *this;
}

GroupModel& GroupModel::setMute(bool mute)
{
    if (d->m_mute == mute)
        return *this;

    d->m_mute = mute;
    emit muteChanged(mute);

    return *this;
}

GroupModel &GroupModel::setPhotoType(PhotoType photoType)
{
    if (d->m_photoType == photoType)
        return *this;

    d->m_photoType = photoType;
    emit photoTypeChanged(photoType);

    return *this;
}

X_END_NAMESPACE
